<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #checkPerson="{ text }">
        <template v-for="tag in text" :key="tag">
          <Tag v-if="tag" color="green" style="margin-left: 2px">
            {{ tag }}
          </Tag>
        </template>
      </template>

      <template #accompanyPerson="{ text }">
        <template v-for="tag in text" :key="tag">
          <Tag v-if="tag" color="cyan" style="margin-left: 2px">
            {{ tag }}
          </Tag>
        </template>
      </template>

      <template #recordPerson="{ text }">
        <template v-for="tag in text" :key="tag">
          <Tag v-if="tag" color="pink" style="margin-left: 2px">
            {{ tag }}
          </Tag>
        </template>
      </template>

      <template #location="{ text }">
        <template v-for="tag in text" :key="tag">
          <Tag v-if="tag" color="blue" style="margin-left: 2px">
            {{ tag }}
          </Tag>
        </template>
      </template>
      <template #accompanyType="{ text }">
        {{ getName(text) }}
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              onClick: handleEdit.bind(null, record),
              label: '编辑',
            },
            {
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
              label: '删除',
            },
            {
              color: 'success',
              onClick: handleDownload.bind(null, record),
              label: '导出',
            },
            {
              color: 'warning',
              onClick: handleDetail.bind(null, record),
              label: '详情',
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer v-if="isShow" @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { delTask, getTaskList } from '/@/api/busi/record';
  import { Tag } from 'ant-design-vue';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import { columns, searchFormSchema } from './role.data';
  import { getCorpTypeList } from '/@/api/busi/corp';

  export default defineComponent({
    name: 'RecordManager',
    components: { BasicTable, RoleDrawer, TableAction, Tag },
    setup() {
      const isShow = ref(true);
      const result = ref();
      const downloadParams = ref();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '检查记录列表',
        api: getTaskList,
        beforeFetch: async (params) => {
          downloadParams.value = params;
          result.value = await getCorpTypeList({});
          return params;
        },
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: false,
        bordered: true,
        showIndexColumn: true,
        pagination: true,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        isShow.value = false;
        nextTick(() => {
          isShow.value = true;
        });
        openDrawer(true, {
          isUpdated: false,
        });
      }

      function handleEdit(record: Recordable) {
        isShow.value = false;
        nextTick(() => {
          isShow.value = true;
        });
        openDrawer(true, {
          record,
          isUpdated: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await delTask({ id: record.id });
        await reload();
      }

      function handleDetail(record: Recordable) {
        isShow.value = false;
        nextTick(() => {
          isShow.value = true;
        });
        openDrawer(true, {
          record,
          isDetail: true,
          isUpdated: true,
        });
      }

      function handleSuccess() {
        reload();
      }

      async function handleDownload() {
        // const result = await getTaskList(downloadParams.value);
        // const res = result.resultData.list;
      }

      function getName(id) {
        const data = result.value.returnData.list.find((e) => {
          return e.id === id;
        });
        let text = '未知';
        if (data) {
          text = data.name;
          return text;
        }
        return text;
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleDownload,
        handleDetail,
        isShow,
        getName,
      };
    },
  });
</script>
